[#include "/includes/_layout.html"/]
[#macro script_import]
<script type="text/javascript" src="${webctx}/resources/js/jquery.lSelect.js"></script>
[/#macro]
[#macro css]
<style type="text/css">
.col-md-5 select{
 width: 30%;
 float: left;
}
</style>
[/#macro]
[@layout]
<div class="wrapper wrapper-content">
	<div class="row animated fadeInLeft">
	[#include "/includes/menu-store.html" /]
	<div class="col-sm-11">
	<div class="row ibox-content">
	<div class="form-horizontal" style="padding-top: 10px;">
		<div>
			<input type="hidden" id="addr_id" name="addrId" value="${sendAddr.id}"/>
			<div class="form-group">
	            <label class="col-md-2 control-label">联系人<span style="color: red;"><em>*</em></span></label>
	            <div class="col-md-5" id="error_contact_name">
	                <input type="text" id="contact_name" name="contact_name" class="form-control" value="${sendAddr.contact_name}"/>
	                <label class="control-label" for="contact_name"></label>
	            </div>
	        </div>
	        <div class="form-group">
	            <label class="col-md-2 control-label">联系人手机<span style="color: red;"><em>*</em></span></label>
	            <div class="col-md-5" id="error_phone">
	                <input type="text" id="phone" name="phone" class="form-control" value="${sendAddr.phone}"/>
	                <label class="control-label" for="phone"></label>
	            </div>
	        </div>
	        <div class="form-group">
	            <label class="col-md-2 control-label">省市区<span style="color: red;"><em>*</em></span></label>
	            <div class="col-md-5" id="error_areaId">
	                <input type="hidden" id="areaId" value="${sendAddr.area_id}"  name="areaId" treePath="${areaPath}"/>
	                <label class="control-label" for="areaId"></label>
	            </div>
	        </div>
	        <div class="form-group">
	            <label class="col-md-2 control-label">详细地址<span style="color: red;"><em>*</em></span></label>
	            <div class="col-md-5" id="error_addr">
	                <input type="text" id="addr" name="addr" class="form-control" value="${sendAddr.addr}"/>
	                <label class="control-label" for="addr"></label>
	            </div>
	        </div>
	        <div class="form-group">
	            <label class="col-md-2 control-label">公司名称</label>
	            <div class="col-md-5" id="error_seller_company">
	                <input type="text" id="seller_company" name="seller_company" class="form-control" value="${sendAddr.seller_company}"/>
	                <label class="control-label" for="seller_company"></label>
	            </div>
	        </div>
	        <div class="form-group">
	            <label class="col-md-2 control-label">邮编</label>
	            <div class="col-md-5" id="error_zip_code">
	                <input type="text" id="zip_code" name="zip_code" class="form-control" value="${sendAddr.zip_code}"/>
	                <label class="control-label" for="zip_code"></label>
	            </div>
	        </div>
	        <div class="form-group">
	            <label class="col-md-2 control-label">备注</label>
	            <div class="col-md-5" id="error_memo">
	                <input type="text" id="memo" name="memo" class="form-control" value="${sendAddr.memo}"/>
	                <label class="control-label" for="zip_memo"></label>
	            </div>
	        </div>
		</div>
		<div class="panel-default">
		  	<div class="panel-body" align="center">
				<button type="button" onclick="saveAddr();" class="btn btn-success btn-sm">保存</button>
		  	</div>
	  	</div>
  	</div>
</div>
</div>
</div>
</div>
[/@layout]
<script type="text/javascript">
$().ready(function() {
	var $areaId = $("#areaId");
	// 地区选择
	$areaId.lSelect({
		url:"${webctx}/area/list"
	});
});
function saveAddr(){
	var params={}, error = {};
	var contact_name = $("#contact_name").val(), addr = $("#addr").val(), phone = $("#phone").val()
	, memo = $("#memo").val(), seller_company = $("#seller_company").val(), zipCode = $("#zip_code").val()
	, addrId = $("#addr_id").val(),areaId = $("#areaId").val();
	params.addrId=addrId;
	params.memo=memo;
	params.sellerCompany=seller_company;
	params.zipCode=zipCode;
	if($.trim(contact_name) =="") {error.error_contact_name = "联系人不能为空"; } else {error.error_contact_name=""; params.contactName = $.trim(contact_name);}
	if($.trim(addr) =="") {error.error_addr = "详细地址不能为空"; } else {error.error_addr=""; params.addr = $.trim(addr);}
	if($.trim(phone) =="") {error.error_phone = "联系人手机不能为空"; } else {error.error_phone=""; params.phone = $.trim(phone);}
	if($.trim(areaId) =="") {error.error_areaId = "省市区不能为空"; } else {error.error_areaId=""; params.areaId = $.trim(areaId);}
	var hasError = false;
	for(var key in error){
		if(error[key]!=""){
			if(!hasError) hasError = true;
			$("#"+key).addClass("has-error");
			$("#"+key).find("label").text(error[key]);
		}else{
			$("#"+key).removeClass("has-error");
			$("#"+key).find("label").empty();
		}
	}
	//主表信息不完整，返回
	if(hasError) return false;
	var country=""
	$(".col-md-5 select").each(function(i, obj){
		var options=$(obj).find("option:selected");
			if(i==1){
				params.city=options.text();	
			}
			if(i==2){
				country=options.text();	
			}
			if(i==0){
				params.province=options.text();	
			}	
	});
	params.country=country;
	$(".main").mask("正在保存...");
	$.post(obz.ctx+"/shop/saveaddr", params, function(json) {
			$(".main").unmask();
			if(json.code!=200){
				obz.error(json.msg);
			}else{
				obz.msg('保存成功', function(){
					location.reload();
				});						
			}
	},"json");
}
</script>
<script type="text/javascript">
	Template.init("#menu-shop");
	Template.initSecond("#menu-addr");
</script>
</body>
</html>